/*
 *    Copyright 2011-2020 enniu.com
 */
package com.zhanghaijun.dao;

import com.zhanghaijun.domain.ZUserBean;
import com.zhanghaijun.util.EhcacheUtil;
import com.zhanghaijun.util.MyCollectionsUtil;
import com.zhanghaijun.util.PrintUtil;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.ehcache.EhCacheCacheManager;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import java.util.*;


/**
* 
*
* @author zhanghaijun 2015-07-21
* E-mail:zhanghaijun8755018@163.com
*/
@Service
public class ZUserDaoImpl{

    @Autowired
    private SqlSession sqlSessionMisc;

    private static final String SQL_Z_USER_INSERT = "ZUserDao.insert";
    private static final String SQL_Z_USER_UPDATE = "ZUserDao.update";
    private static final String SQL_Z_USER_DELETE = "ZUserDao.deleteById";
    private static final String SQL_Z_USER_SELECT_BY_ID = "ZUserDao.selectById";
    private static final String SQL_Z_USER_SELECT_BY_USERNAME_AND_PASSWORD = "ZUserDao.selectByUserNameAndPassword";
    private static final String SQL_Z_USER_SELECT_LIST = "ZUserDao.selectList";

    /**
     *@param zUserBean
     * 增加
     */
	public int insert(ZUserBean zUserBean){
        return sqlSessionMisc.insert(SQL_Z_USER_INSERT, zUserBean);
	}
	
    /**
     * 更新
     * @param zUserBean
     * @return
     */
	public int update(ZUserBean zUserBean) {
        return sqlSessionMisc.update(SQL_Z_USER_UPDATE, zUserBean);
	}
	
	/**
     * 根据主键删除
	 * @param id
	 */
	public int deleteById(Long id) {
        return sqlSessionMisc.delete(SQL_Z_USER_DELETE, id);
	}
	
    /**
     * 根据主键查询
     * @param id
     * @return
     */
	public ZUserBean selectById(Long id) {
        return (ZUserBean)sqlSessionMisc.selectOne(SQL_Z_USER_SELECT_BY_ID,id);
	}

    /**
     * 获取列表
     * @return
     */
    public List<ZUserBean> selectList(int id){
        String key = LIST_KEY_PREFIX + id;
        Object obj = EhcacheUtil.get(cacheName,key);
        if(obj!=null){
            PrintUtil.println("from cache");
            return (List<ZUserBean>)obj;
        }
        List<ZUserBean> list = sqlSessionMisc.selectList(SQL_Z_USER_SELECT_LIST);
        if(MyCollectionsUtil.isNotEmptyList(list)){
            //EhcacheUtil.put(cacheName,key,list,10);
            EhcacheUtil.put(cacheName,key,list);
        }
        PrintUtil.println("from db");
        return  list;
    }

    public ZUserBean login(String username,String password){
        Map<String,Object> params = new HashMap<String,Object>();
        params.put("username",username);
        params.put("password", password);
        return sqlSessionMisc.selectOne(SQL_Z_USER_SELECT_BY_USERNAME_AND_PASSWORD,params);
    }



    private static String LIST_KEY_PREFIX = "ZUserDaoImpl_getList_";

    private static String cacheName="zUser";

}